export var NestedIterator = function (nestedList) {
  this.nums = []
  this.index = 0
  if (nestedList % 1 === 0) {
    this.nums.push(nestedList)
  } else {
    for (const node of nestedList) {
      this.nums.push(...dfs(node))
    }
  }
}

function dfs(nestedInteger) {
  const res = []
  if (nestedInteger.isInteger()) {
    res.push(nestedInteger.getInteger())
  } else {
    for (const child of nestedInteger.getList()) {
      res.push(...dfs(child))
    }
  }
  return res
}

NestedIterator.prototype.hasNext = function () {
  return this.index < this.nums.length
}

NestedIterator.prototype.next = function () {
  return this.nums[this.index++]
}

NestedIterator.prototype.isInteger = function () {
  if (this.nums[this.index] !== undefined) {
    return this.nums[this.index] % 1 === 0
  }
}

NestedIterator.prototype.getInteger = function () {
  return this.isInteger ? this.nums[this.index] : null
}

NestedIterator.prototype.getList = function () {
  return this.isInteger ? null : this.nums.slice(this.index)
}

NestedIterator.prototype.setInteger = function (value) {
  this.nums[this.index] = value
}

NestedIterator.prototype.add = function (elem) {
  this.nums.splice(this.index, 0, elem)
}
